import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:tijarat/screens/profile_screen.dart';
import 'package:tijarat/screens/settings_screen.dart';
import 'package:tijarat/screens/login_screen.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return SizedBox(
          width: constraints.maxWidth,
          child: Column(
            children: [
              _buildTopBar(),
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      _buildStatisticsCards(),
                      _buildQuickAccessButtons(),
                      _buildCharts(),
                      _buildRecentActivities(),
                      _buildStockReports(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  // يۇقىرى قىسىم
  Widget _buildTopBar() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // ئىزدەش قىسمى
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(left: 16),
              decoration: BoxDecoration(
                color: Colors.grey[100],
                borderRadius: BorderRadius.circular(4),
              ),
              child: TextField(
                textDirection: TextDirection.rtl,
                decoration: InputDecoration(
                  hintText: 'ئىزدەش...',
                  hintTextDirection: TextDirection.rtl,
                  prefixIcon: const Icon(Icons.search, color: Colors.grey),
                  border: InputBorder.none,
                  contentPadding:
                      const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                ),
              ),
            ),
          ),

          // كىچىك كۇنۇپكىلار
          Row(
            children: [
              PopupMenuButton<String>(
                icon: Icon(Icons.language, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildLanguageMenuItem(
                    flag: '🇨🇳',
                    language: 'ئۇيغۇرچە',
                    isSelected: true,
                  ),
                  _buildLanguageMenuItem(
                    flag: '🇨🇳',
                    language: '中文',
                    isSelected: false,
                  ),
                  _buildLanguageMenuItem(
                    flag: '🇺🇸',
                    language: 'English',
                    isSelected: false,
                  ),
                ],
                onSelected: (String value) {
                  // بۇ يەردە تىل ئۆزگەرتىش قىلىنىدۇ
                },
              ),
              const SizedBox(width: 8),
              PopupMenuButton<String>(
                icon: Icon(Icons.brush, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildThemeMenuItem(
                    icon: Icons.light_mode,
                    title: 'يورۇق ئۇسلۇب',
                    isSelected: true,
                  ),
                  _buildThemeMenuItem(
                    icon: Icons.dark_mode,
                    title: 'قارا ئۇسلۇب',
                    isSelected: false,
                  ),
                  _buildThemeMenuItem(
                    icon: Icons.settings_system_daydream,
                    title: 'سىستېما ئۇسلۇبى',
                    isSelected: false,
                  ),
                ],
                onSelected: (String value) {
                  // بۇ يەردە ئۇسلۇب ئۆزگەرتىش قىلىنىدۇ
                },
              ),
              const SizedBox(width: 8),
              Stack(
                children: [
                  IconButton(
                    icon: Icon(Icons.notifications, color: Colors.grey[600]),
                    onPressed: () {
                      _showNotificationsDialog();
                    },
                  ),
                  Positioned(
                    right: 8,
                    top: 8,
                    child: Container(
                      padding: const EdgeInsets.all(4),
                      decoration: const BoxDecoration(
                        color: Colors.red,
                        shape: BoxShape.circle,
                      ),
                      child: const Text(
                        '3',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(width: 8),
              PopupMenuButton<String>(
                icon: Icon(Icons.person, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildUserMenuItem(
                    icon: Icons.account_circle,
                    title: 'شەخسىي ئۇچۇر',
                  ),
                  _buildUserMenuItem(
                    icon: Icons.settings,
                    title: 'تەڭشەكلەر',
                  ),
                  const PopupMenuDivider(),
                  _buildUserMenuItem(
                    icon: Icons.logout,
                    title: 'چېكىنىش',
                  ),
                ],
                onSelected: (String value) {
                  switch (value) {
                    case 'شەخسىي ئۇچۇر':
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const ProfileScreen(),
                        ),
                      );
                      break;
                    case 'تەڭشەكلەر':
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const SettingsScreen(),
                        ),
                      );
                      break;
                    case 'چېكىنىش':
                      showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                          title: const Text('چېكىنىش'),
                          content: const Text('راستىنلا چېكىنمەكچىمۇ؟'),
                          actions: [
                            TextButton(
                              onPressed: () => Navigator.pop(context),
                              child: const Text('ياق'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                                Navigator.pushReplacement(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => const LoginScreen(),
                                  ),
                                );
                              },
                              child: const Text('ھەئە'),
                            ),
                          ],
                        ),
                      );
                      break;
                  }
                },
              ),
            ],
          ),
        ],
      ),
    );
  }

  // كىچىك كۇنۇپكا
  Widget _buildIconButton(IconData icon) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: () {},
        borderRadius: BorderRadius.circular(4),
        child: Container(
          padding: const EdgeInsets.all(8),
          child: Icon(icon, color: Colors.grey[600]),
        ),
      ),
    );
  }

  // ستاتىستىكا كارتىلىرى
  Widget _buildStatisticsCards() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، ھەر قاتاردىكى كارتا سانىنى بەلگىلەش
    final screenWidth = MediaQuery.of(context).size.width;
    final crossAxisCount = screenWidth < 800
        ? 2
        : screenWidth < 1200
            ? 3
            : 5;

    return GridView.count(
      crossAxisCount: crossAxisCount,
      crossAxisSpacing: 24,
      mainAxisSpacing: 24,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        _buildStatCard(
          title: 'كىرىم-چىقىم',
          value: '¥ 45,678',
          change: '+12.5%',
          color: Colors.blue,
        ),
        _buildStatCard(
          title: 'سودا سانى',
          value: '1,234',
          change: '+8.3%',
          color: Colors.amber,
        ),
        _buildStatCard(
          title: 'ئامبار مىقدارى',
          value: '5,678',
          change: '-2.1%',
          color: Colors.blue.shade500,
        ),
        _buildStatCard(
          title: 'كىرگۈزگەن مال',
          value: '789',
          change: '+5.7%',
          color: Colors.red,
        ),
        _buildStatCard(
          title: 'نىسى تەپسىلاتى',
          value: '234',
          change: '+15.2%',
          color: Colors.green,
        ),
      ],
    );
  }

  // بىر ستاتىستىكا كارتى
  Widget _buildStatCard({
    required String title,
    required String value,
    required String change,
    required Color color,
  }) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: const TextStyle(
              color: Colors.white,
              fontSize: 16,
              fontWeight: FontWeight.w500,
            ),
            overflow: TextOverflow.ellipsis,
          ),
          const Spacer(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Expanded(
                child: Text(
                  value,
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              const SizedBox(width: 8),
              Text(
                change,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 14,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // تېز كىرىش كۇنۇپكىلىرى
  Widget _buildQuickAccessButtons() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، ھەر قاتاردىكى كۇنۇپكا سانىنى بەلگىلەش
    final screenWidth = MediaQuery.of(context).size.width;
    final crossAxisCount = screenWidth < 800
        ? 3
        : screenWidth < 1200
            ? 4
            : 8;

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: GridView.count(
        crossAxisCount: crossAxisCount,
        crossAxisSpacing: 16,
        mainAxisSpacing: 16,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        children: [
          _buildQuickButton(Icons.shopping_bag, 'مال كىرگۈزۈش'),
          _buildQuickButton(Icons.local_shipping, 'مال چىقىرىش'),
          _buildQuickButton(Icons.assignment_turned_in, 'مال تاپشۇرۇش'),
          _buildQuickButton(Icons.search, 'ئامبار تەكشۈرۈش'),
          _buildQuickButton(Icons.inventory_2, 'مال ئۇچۇرى'),
          _buildQuickButton(Icons.people, 'خېرىدار ئۇچۇرى'),
          _buildQuickButton(Icons.receipt_long, 'سېتىش تالونى'),
          _buildQuickButton(Icons.description, 'كىرگۈزۈش تالونى'),
        ],
      ),
    );
  }

  // بىر تېز كىرىش كۇنۇپكىسى
  Widget _buildQuickButton(IconData icon, String title) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: () {},
        borderRadius: BorderRadius.circular(8),
        child: Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                color: Colors.blue,
                size: 28,
              ),
              const SizedBox(height: 8),
              Text(
                title,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.grey[800],
                  fontSize: 12,
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        ),
      ),
    );
  }

  // دىئاگرامما قىسمى
  Widget _buildCharts() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، دىئاگراممىلارنى تىك ياكى يانتۇ تىزىش
    final screenWidth = MediaQuery.of(context).size.width;
    final isWideScreen = screenWidth >= 1200;

    if (isWideScreen) {
      // كەڭ ئېكران ئۈچۈن يانتۇ تىزىش
      return Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildSalesChartContainer(),
          const SizedBox(width: 24),
          _buildProductsChartContainer(),
        ],
      );
    } else {
      // تار ئېكران ئۈچۈن تىك تىزىش
      return Column(
        children: [
          _buildSalesChartContainer(),
          const SizedBox(height: 24),
          _buildProductsChartContainer(),
        ],
      );
    }
  }

  // تىجارەت دىئاگراممىسى كونتېينېرى
  Widget _buildSalesChartContainer() {
    return Expanded(
      child: Container(
        height: 400,
        padding: const EdgeInsets.all(24),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.1),
              blurRadius: 4,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              'تىجارەت ئەھۋالى',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
              ),
            ),
            const SizedBox(height: 32),
            Expanded(
              child: _buildSalesLineChart(),
            ),
          ],
        ),
      ),
    );
  }

  // بازارلىق ماللار دىئاگراممىسى كونتېينېرى
  Widget _buildProductsChartContainer() {
    return Expanded(
      child: Container(
        height: 400,
        padding: const EdgeInsets.all(24),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.1),
              blurRadius: 4,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              'بازارلىق ماللار',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w500,
              ),
            ),
            const SizedBox(height: 32),
            Expanded(
              child: _buildProductsPieChart(),
            ),
          ],
        ),
      ),
    );
  }

  // سېتىش دىئاگراممىسى
  Widget _buildSalesLineChart() {
    return LineChart(
      LineChartData(
        gridData: FlGridData(
          show: true,
          drawVerticalLine: true,
          horizontalInterval: 50,
          verticalInterval: 1,
        ),
        titlesData: FlTitlesData(
          rightTitles: AxisTitles(
            sideTitles: SideTitles(showTitles: false),
          ),
          topTitles: AxisTitles(
            sideTitles: SideTitles(showTitles: false),
          ),
          bottomTitles: AxisTitles(
            sideTitles: SideTitles(
              showTitles: true,
              getTitlesWidget: (value, meta) {
                const titles = [
                  '1-ئاي',
                  '2-ئاي',
                  '3-ئاي',
                  '4-ئاي',
                  '5-ئاي',
                  '6-ئاي'
                ];
                final index = value.toInt();
                if (index >= 0 && index < titles.length) {
                  return Padding(
                    padding: const EdgeInsets.only(top: 8.0),
                    child: Text(titles[index]),
                  );
                }
                return const Text('');
              },
              reservedSize: 32,
            ),
          ),
          leftTitles: AxisTitles(
            sideTitles: SideTitles(
              showTitles: true,
              reservedSize: 42,
              getTitlesWidget: (value, meta) {
                return Text(
                  value.toInt().toString(),
                  style: const TextStyle(
                    color: Color(0xff68737d),
                    fontSize: 12,
                  ),
                  textAlign: TextAlign.left,
                );
              },
            ),
          ),
        ),
        borderData: FlBorderData(
          show: true,
          border: Border.all(color: const Color(0xff37434d), width: 1),
        ),
        minX: 0,
        maxX: 5,
        minY: 0,
        maxY: 350,
        lineBarsData: [
          LineChartBarData(
            spots: const [
              FlSpot(0, 120),
              FlSpot(1, 132),
              FlSpot(2, 101),
              FlSpot(3, 134),
              FlSpot(4, 90),
              FlSpot(5, 230),
            ],
            isCurved: true,
            color: Colors.blue,
            barWidth: 3,
            isStrokeCapRound: true,
            dotData: FlDotData(show: false),
            belowBarData: BarAreaData(show: false),
          ),
          LineChartBarData(
            spots: const [
              FlSpot(0, 220),
              FlSpot(1, 182),
              FlSpot(2, 191),
              FlSpot(3, 234),
              FlSpot(4, 290),
              FlSpot(5, 330),
            ],
            isCurved: true,
            color: const Color(0xff0284C7),
            barWidth: 3,
            isStrokeCapRound: true,
            dotData: FlDotData(show: false),
            belowBarData: BarAreaData(show: false),
          ),
        ],
      ),
    );
  }

  // بازارلىق ماللار دىئاگراممىسى
  Widget _buildProductsPieChart() {
    return PieChart(
      PieChartData(
        sectionsSpace: 0,
        centerSpaceRadius: 70,
        sections: [
          PieChartSectionData(
            value: 1048,
            title: 'كومپيۇتېر',
            color: Colors.blue,
            radius: 80,
            titleStyle: const TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          PieChartSectionData(
            value: 735,
            title: 'يانفون',
            color: Colors.green,
            radius: 80,
            titleStyle: const TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          PieChartSectionData(
            value: 580,
            title: 'پرىنتېر',
            color: Colors.red,
            radius: 80,
            titleStyle: const TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          PieChartSectionData(
            value: 484,
            title: 'توركۆرگۈ',
            color: Colors.purple,
            radius: 80,
            titleStyle: const TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          PieChartSectionData(
            value: 300,
            title: 'قاتتىق دىسكا',
            color: Colors.amber,
            radius: 80,
            titleStyle: const TextStyle(
              color: Colors.white,
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  // يېقىنقى پائالىيەتلەر
  Widget _buildRecentActivities() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'ئەڭ يېقىنقى سودا ئەھۋالى',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        const SizedBox(height: 16),
        Container(
          padding: const EdgeInsets.all(24),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.1),
                blurRadius: 4,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            children: [
              _buildActivityItem(
                icon: Icons.shopping_cart,
                iconColor: Colors.blue,
                iconBgColor: Colors.blue.withOpacity(0.1),
                title: 'يېڭى مال كىرگۈزۈلدى',
                subtitle: 'ئەلى ئابدۇللا تەرىپىدىن',
                time: '5 مىنۇت بۇرۇن',
              ),
              const Divider(height: 32),
              _buildActivityItem(
                icon: Icons.check_circle,
                iconColor: Colors.green,
                iconBgColor: Colors.green.withOpacity(0.1),
                title: 'سودا تاماملاندى',
                subtitle: 'مەريەم ئەخمەت تەرىپىدىن',
                time: '15 مىنۇت بۇرۇن',
              ),
              const Divider(height: 32),
              _buildActivityItem(
                icon: Icons.local_shipping,
                iconColor: Colors.amber,
                iconBgColor: Colors.amber.withOpacity(0.1),
                title: 'مال يوللاندى',
                subtitle: 'ئايگۈل تۇرسۇن تەرىپىدىن',
                time: '30 مىنۇت بۇرۇن',
              ),
            ],
          ),
        ),
      ],
    );
  }

  // بىر پائالىيەت ئۇچۇرى
  Widget _buildActivityItem({
    required IconData icon,
    required Color iconColor,
    required Color iconBgColor,
    required String title,
    required String subtitle,
    required String time,
  }) {
    return Row(
      children: [
        Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: iconBgColor,
            borderRadius: BorderRadius.circular(20),
          ),
          child: Icon(
            icon,
            color: iconColor,
          ),
        ),
        const SizedBox(width: 16),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: const TextStyle(
                  fontWeight: FontWeight.w500,
                ),
                overflow: TextOverflow.ellipsis,
              ),
              Text(
                subtitle,
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey[600],
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        ),
        const SizedBox(width: 16),
        Text(
          time,
          style: TextStyle(
            fontSize: 14,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }

  // ئامبار دوكلاتلىرى
  Widget _buildStockReports() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، دوكلاتلارنى تىك ياكى يانتۇ تىزىش
    final screenWidth = MediaQuery.of(context).size.width;
    final isWideScreen = screenWidth >= 1200;

    if (isWideScreen) {
      // كەڭ ئېكران ئۈچۈن يانتۇ تىزىش
      return Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildLowStockContainer(),
          const SizedBox(width: 24),
          _buildOverstockedContainer(),
        ],
      );
    } else {
      // تار ئېكران ئۈچۈن تىك تىزىش
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildLowStockContainer(),
          const SizedBox(height: 24),
          _buildOverstockedContainer(),
        ],
      );
    }
  }

  // ئامباردا ئاز قالغان ماللار كونتېينېرى
  Widget _buildLowStockContainer() {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'ئامباردا ئاز قالغان ماللار',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Column(
              children: [
                _buildStockItem(
                  icon: Icons.warning,
                  iconColor: Colors.red,
                  iconBgColor: Colors.red.withOpacity(0.1),
                  title: 'ThinkPad X1 Carbon',
                  subtitle: 'قالغان سانى: 5 دانە',
                  buttonText: 'مال تولۇقلاش',
                  buttonColor: Colors.blue,
                ),
                const Divider(height: 32),
                _buildStockItem(
                  icon: Icons.warning,
                  iconColor: Colors.red,
                  iconBgColor: Colors.red.withOpacity(0.1),
                  title: 'iPhone 14 Pro',
                  subtitle: 'قالغان سانى: 3 دانە',
                  buttonText: 'مال تولۇقلاش',
                  buttonColor: Colors.blue,
                ),
                const Divider(height: 32),
                _buildStockItem(
                  icon: Icons.warning,
                  iconColor: Colors.red,
                  iconBgColor: Colors.red.withOpacity(0.1),
                  title: 'HP LaserJet Pro',
                  subtitle: 'قالغان سانى: 2 دانە',
                  buttonText: 'مال تولۇقلاش',
                  buttonColor: Colors.blue,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // ئامباردا بېسىلىپ قالغان ماللار كونتېينېرى
  Widget _buildOverstockedContainer() {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'ئامباردا بېسىلىپ قالغان ماللار',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          Container(
            padding: const EdgeInsets.all(24),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.1),
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Column(
              children: [
                _buildStockItem(
                  icon: Icons.inventory,
                  iconColor: Colors.orange,
                  iconBgColor: Colors.orange.withOpacity(0.1),
                  title: 'Dell XPS 15',
                  subtitle: 'قالغان سانى: 150 دانە',
                  buttonText: 'تېزلىتىپ سېتىش',
                  buttonColor: Colors.orange,
                ),
                const Divider(height: 32),
                _buildStockItem(
                  icon: Icons.inventory,
                  iconColor: Colors.orange,
                  iconBgColor: Colors.orange.withOpacity(0.1),
                  title: 'Samsung Galaxy S23',
                  subtitle: 'قالغان سانى: 120 دانە',
                  buttonText: 'تېزلىتىپ سېتىش',
                  buttonColor: Colors.orange,
                ),
                const Divider(height: 32),
                _buildStockItem(
                  icon: Icons.inventory,
                  iconColor: Colors.orange,
                  iconBgColor: Colors.orange.withOpacity(0.1),
                  title: 'Canon PIXMA',
                  subtitle: 'قالغان سانى: 85 دانە',
                  buttonText: 'تېزلىتىپ سېتىش',
                  buttonColor: Colors.orange,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // بىر مال ئۇچۇرى
  Widget _buildStockItem({
    required IconData icon,
    required Color iconColor,
    required Color iconBgColor,
    required String title,
    required String subtitle,
    required String buttonText,
    required Color buttonColor,
  }) {
    return Row(
      children: [
        Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: iconBgColor,
            borderRadius: BorderRadius.circular(20),
          ),
          child: Icon(
            icon,
            color: iconColor,
          ),
        ),
        const SizedBox(width: 16),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: const TextStyle(
                  fontWeight: FontWeight.w500,
                ),
                overflow: TextOverflow.ellipsis,
              ),
              Text(
                subtitle,
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey[600],
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        ),
        const SizedBox(width: 16),
        SizedBox(
          width: 120,
          child: ElevatedButton(
            onPressed: () {},
            style: ElevatedButton.styleFrom(
              backgroundColor: buttonColor,
              foregroundColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4),
              ),
            ),
            child: Text(
              buttonText,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
      ],
    );
  }

  // تىل تاللاش تۈرى
  PopupMenuItem<String> _buildLanguageMenuItem({
    required String flag,
    required String language,
    required bool isSelected,
  }) {
    return PopupMenuItem<String>(
      value: language,
      child: Row(
        children: [
          Text(flag, style: const TextStyle(fontSize: 24)),
          const SizedBox(width: 12),
          Text(language),
          if (isSelected) ...[
            const Spacer(),
            const Icon(Icons.check_circle, color: Colors.blue, size: 20),
          ],
        ],
      ),
    );
  }

  // ئۇسلۇب تاللاش تۈرى
  PopupMenuItem<String> _buildThemeMenuItem({
    required IconData icon,
    required String title,
    required bool isSelected,
  }) {
    return PopupMenuItem<String>(
      value: title,
      child: Row(
        children: [
          Icon(icon, color: Colors.grey[600], size: 20),
          const SizedBox(width: 12),
          Text(title),
          if (isSelected) ...[
            const Spacer(),
            const Icon(Icons.check_circle, color: Colors.blue, size: 20),
          ],
        ],
      ),
    );
  }

  // ئۇقتۇرۇش كۆزنىكىنى كۆرسىتىش
  void _showNotificationsDialog() {
    showGeneralDialog(
      context: context,
      barrierDismissible: true,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.black54,
      transitionDuration: const Duration(milliseconds: 300),
      pageBuilder: (context, animation1, animation2) => Container(),
      transitionBuilder: (context, animation1, animation2, child) {
        final curvedAnimation = CurvedAnimation(
          parent: animation1,
          curve: Curves.easeInOutBack,
          reverseCurve: Curves.easeInOutBack,
        );

        return ScaleTransition(
          scale: Tween<double>(begin: 0.8, end: 1.0).animate(curvedAnimation),
          child: FadeTransition(
            opacity:
                Tween<double>(begin: 0.0, end: 1.0).animate(curvedAnimation),
            child: Dialog(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16),
              ),
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeInOut,
                width: 400,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: const BoxDecoration(
                        color: Colors.blue,
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(16),
                          topRight: Radius.circular(16),
                        ),
                      ),
                      child: const Row(
                        children: [
                          Icon(Icons.notifications, color: Colors.white),
                          SizedBox(width: 8),
                          Text(
                            'ئۇقتۇرۇشلار',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                    ),
                    ListView(
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(16),
                      children: [
                        _buildNotificationItem(
                          'يېڭى مال كىرگۈزۈلدى',
                          'ThinkPad X1 Carbon دىن 10 دانە كىرگۈزۈلدى',
                          '5 مىنۇت بۇرۇن',
                          Colors.blue,
                        ),
                        const Divider(),
                        _buildNotificationItem(
                          'مال تۈگەپ قالدى',
                          'iPhone 14 Pro نىڭ ئامباردىكى سانى 3 دانە قالدى',
                          '15 مىنۇت بۇرۇن',
                          Colors.red,
                        ),
                        const Divider(),
                        _buildNotificationItem(
                          'يېڭى سودا',
                          'HP LaserJet Pro دىن 2 دانە سېتىلدى',
                          '30 مىنۇت بۇرۇن',
                          Colors.green,
                        ),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.all(16),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          TextButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: const Text('تاقاش'),
                          ),
                          const SizedBox(width: 8),
                          ElevatedButton(
                            onPressed: () {
                              // ھەممىنى ئوقۇلدى قىلىش
                              Navigator.of(context).pop();
                            },
                            child: const Text('ھەممىنى ئوقۇلدى'),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  // ئۇقتۇرۇش تۈرى
  Widget _buildNotificationItem(
    String title,
    String message,
    String time,
    Color color,
  ) {
    return ListTile(
      contentPadding: EdgeInsets.zero,
      leading: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: color.withOpacity(0.1),
          shape: BoxShape.circle,
        ),
        child: Icon(Icons.notifications, color: color),
      ),
      title: Text(
        title,
        style: const TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 4),
          Text(message),
          const SizedBox(height: 4),
          Text(
            time,
            style: TextStyle(
              color: Colors.grey[600],
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }

  // ئىشلەتكۈچى تىزىملىك تۈرى
  PopupMenuItem<String> _buildUserMenuItem({
    required IconData icon,
    required String title,
  }) {
    return PopupMenuItem<String>(
      value: title,
      child: Row(
        children: [
          Icon(icon, color: Colors.grey[600], size: 20),
          const SizedBox(width: 12),
          Text(title),
        ],
      ),
    );
  }
}
